package com.qing.library_manager.service.Impl.bookImpl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qing.library_manager.dao.book.bookMapper;
import com.qing.library_manager.domain.dto.BookDTO;
import com.qing.library_manager.domain.entity.Book;
import com.qing.library_manager.domain.query.BookQuery;
import com.qing.library_manager.domain.query.BookQueryByName;
import com.qing.library_manager.domain.query.BookQueryByPage;
import com.qing.library_manager.service.manager.book.bookService;
import com.qing.library_manager.common.util.ResponseResult;
import com.qing.library_manager.common.util.SnowFlakeIdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class bookServiceImpl extends ServiceImpl<bookMapper, Book> implements bookService {

    /**
     * 注入mapper
     */
    private final bookMapper bookMapper;
    private final SnowFlakeIdWorker snowFlakeIdWorker;
    
    public bookServiceImpl(com.qing.library_manager.dao.book.bookMapper bookMapper,
                           SnowFlakeIdWorker snowFlakeIdWorker) {
        this.bookMapper = bookMapper;
        this.snowFlakeIdWorker = snowFlakeIdWorker;
    }


    /**
     * 新增图书
     *
     * @param bookDTO
     * @return
     */
    @Override
    public ResponseResult increaseBook(BookDTO bookDTO) {
        /**
         * 判断此本书存在
         * 存在的话就返回操作失败
         */
        int exists = bookMapper.queryBookExists(new BookQueryByName(bookDTO.getBookName(), bookDTO.getBookAuthorName()));
        System.out.println(exists);
        if (exists > 0) {
            return ResponseResult.FAILED();
        }
        /**
         * 补全Book的数据
         */
        Book book = new Book();
        book.setBookId(String.valueOf(snowFlakeIdWorker.nextId()));
        book.setBookName(bookDTO.getBookName().trim());
        book.setBookAuthorName(bookDTO.getBookAuthorName().trim());
        book.setBookPrice(bookDTO.getBookPrice());
        book.setBookPlace(bookDTO.getBookPlace().trim());
        book.setBookRemark(bookDTO.getBookRemark().trim());
        book.setBookStatus("1".trim());
        int flag = bookMapper.insert(book);
        return flag > 0 ? ResponseResult.SUCCESS() : ResponseResult.FAILED();
    }

    /**
     * 更新图书
     *
     * @param bookDTO
     * @return
     */
    @Override
    public ResponseResult updateBookByName(BookDTO bookDTO) {
        int i = bookMapper.updateBookByName(bookDTO);
        return i > 0 ? ResponseResult.SUCCESS() : ResponseResult.FAILED();
    }

    /**
     * 删除图书
     *
     * @param bookShowId
     * @return
     */
    @Override
    public ResponseResult deleteBookByName(int bookShowId) {
        int i = bookMapper.deleteBookByName(bookShowId);
        return i > 0 ? ResponseResult.SUCCESS() : ResponseResult.FAILED();
    }


    /**
     * 查询书籍根据特定的条件
     *
     * @return
     */
    @Override
    public ResponseResult queryAllBookInit(BookQueryByName bookQueryByName) {
        List<BookDTO> bookquery = bookMapper.queryAllBookInit(bookQueryByName);
        System.out.println(bookquery);
        return bookquery != null ? ResponseResult.SUCCESS(bookquery) : ResponseResult.FAILED();
    }

    /**
     * 根据id查询书籍信息
     *
     * @param bookShowId
     * @return
     */
    @Override
    public ResponseResult queryBookById(int bookShowId) {
        BookDTO bookDTO = bookMapper.queryBookById(bookShowId);
        return bookDTO != null ? ResponseResult.SUCCESS(bookDTO) : ResponseResult.FAILED();
    }

    /**
     * 分页查询数据
     *
     * @param bookQueryByPage
     * @return
     */
    @Override
    public ResponseResult queryBookByPage(int page, int pageSize, BookQueryByPage bookQueryByPage) {
        if (page <= 0 || pageSize <= 0) {
            return ResponseResult.FAILED();
        }
        Page<BookDTO> pages = new Page<>(page, pageSize);
        IPage<BookDTO> bookIPage = bookMapper.queryBookByPage(pages, bookQueryByPage);
        return bookIPage != null ? ResponseResult.SUCCESS(bookIPage) : ResponseResult.FAILED();
    }

    /**
     * 获得全部数据
     *
     * @return
     */
    @Override
    public ResponseResult queryAllBook() {
        List<BookDTO> bookDTOS = bookMapper.queryAllBook();
        return bookDTOS != null ? ResponseResult.SUCCESS(bookDTOS) : ResponseResult.FAILED();
    }
}
